package com.xhsj.user.hr.system.web;

import com.xhsj.user.hr.base.BaseParam;
import com.xhsj.user.hr.process.dto.hrEntryProcess.StartEntryApplyDto;
import com.xhsj.user.hr.process.dto.processDefinition.HandleAuditDto;
import com.xhsj.user.hr.system.dto.SavePostEntryDto;
import com.xhsj.user.hr.system.query.FindDimissionListQuery;
import com.xhsj.user.hr.system.vo.DimissionApplyAuditTaskVo;
import com.xhsj.user.hr.system.vo.ZhaopinDemandApplyAuditTaskVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import com.xhsj.user.utils.Message;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;

import org.springframework.web.bind.annotation.RestController;


import com.xhsj.user.hr.system.service.TbHrApprovalDimissionApplyService;
import com.xhsj.user.hr.system.entity.TbHrApprovalDimissionApply;


/**
 * <p>
 * hr流程-离职申请表 前端控制器
 * </p>
 *
 * @author suxiaolin
 * @since Sep 8, 2020 11:41:49 AM
 */
@Slf4j
@Api(tags = "hr流程-离职申请表")
@RestController
@RequestMapping("/system/tbHrApprovalDimissionApply")
public class TbHrApprovalDimissionApplyController {

	@Autowired
    public TbHrApprovalDimissionApplyService service;


	/**
	 * 保存/修改
	 * @author suxiaolin
	 * @date Sep 8, 2020 11:41:49 AM
	 */
	@ApiOperation(value = "hr流程-离职申请表,保存/修改", notes = "hr流程-离职申请表,保存和修改方法")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
    @PreAuthorize("hasAnyAuthority('tb:hr:approval:dimission:apply:edit','tb:hr:personnel:to:do:edit')")
	public Message save(@RequestBody TbHrApprovalDimissionApply data){
    	try {
    		service.saveData(data);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
        	return Message.error(1,e.getMessage());
		}

	}

	/**
	 * 通过id删除数据
	 * @author suxiaolin
	 * @date Sep 8, 2020 11:41:49 AM
	 */
	@ApiOperation(value = "hr流程-离职申请表删除", notes = "通过id删除hr流程-离职申请表信息。")
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
    @PreAuthorize("hasAnyAuthority('tb:hr:approval:dimission:apply:delete','tb:hr:personnel:to:do:delete')")
	public Message delete(@RequestParam(value="id") Long id) {
		try {
			service.removeById(id);
        	return Message.success("成功");
		} catch (Exception e) {
			log.error("删除失败:{}",e);
            return Message.error(1,"失败");
		}

	}

	/**
	 * 批量删除
	 * @author suxiaolin
	 * @date Sep 8, 2020 11:41:49 AM
	 */
	@ApiOperation(value = "批量删除", notes = "批量删除。")
	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('tb:hr:approval:dimission:apply:delete')")
	public Message batchDelete(@RequestBody List<Long> list) {
		try {
			service.removeByIds(list);
    		return Message.success("成功");
		} catch (Exception e) {
    		log.error("删除失败:{}",e);
    		return Message.error(1,"失败");
		}
    }

    /**
    * 分页查询数据
    * @author suxiaolin
    * @date Sep 8, 2020 11:41:49 AM
    */
	@ApiOperation(value = "分页查询hr流程-离职申请表list", notes = "带参数查询,{\"page\": 1,\"size\": 5,......}。支持模糊查询")
	@RequestMapping(value="/findList",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:hr:approval:dimission:apply:view')")
	public Message findList(@RequestBody BaseParam data) {
        try {
        	IPage<TbHrApprovalDimissionApply> selectPage = service.findApplyList(data);
    		return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
        } catch (Exception e) {
			log.error("查询异常:{}",e);
    		return Message.error(1,"失败");
        }
	}


	/**
	 * 查询人事待办列表
	 * @author 苏小林
	 * @date 2020/11/13 14:10
	 * @param: data
	 * @return: com.xhsj.user.utils.Message
	 */
	@ApiOperation(value = "查询人事待办列表", notes = "查询人事待办列表")
	@RequestMapping(value="/findToDoList",method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('tb:hr:personnel:to:do:view')")
	public Message findToDoList(@RequestBody BaseParam data) {
		try {
			IPage<TbHrApprovalDimissionApply> selectPage = service.findToDoList(data);
			return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
		} catch (Exception e) {
			log.error("查询异常:{}",e);
			return Message.error(1,"失败");
		}
	}

	/**
	 * 离职库列表
	 * @author 苏小林
	 * @date 2020/9/15 17:17
	 */
	@ApiOperation(value = "离职库列表", notes = "离职库列表")
	@RequestMapping(value="/findDimissionList",method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('tb:hr:approval:dimission:library:view')")
	public Message findDimissionList(@RequestBody FindDimissionListQuery data) {

		try {
			IPage<TbHrApprovalDimissionApply> selectPage = service.findDimissionList(data);
			return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
		} catch (Exception e) {
			log.error("查询异常:{}",e);
			return Message.error(1,"失败");
		}
	}


	/**
	 * 查询交接列表
	 * @author 苏小林
	 * @date 2020/9/9 17:39
	 * @param: data
	 * @return: com.xhsj.user.utils.Message
	 */
	@ApiOperation(value = "人事待办查询交接列表", notes = "人事待办查询交接列表")
	@RequestMapping(value="/findHandoverList",method = RequestMethod.POST)
	@PreAuthorize("hasAnyAuthority('tb:hr:handover:to:do:view')")
	public Message findHandoverList(@RequestBody BaseParam data) {
		try {
			IPage<TbHrApprovalDimissionApply> selectPage = service.findHandoverList(data);
			return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
		} catch (Exception e) {
			log.error("查询异常:{}",e);
			return Message.error(1,"失败");
		}
	}

	/**
	 * 查询我的交接列表
	 * @author 苏小林
	 * @date 2020/9/9 17:39
	 * @param: data
	 * @return: com.xhsj.user.utils.Message
	 */
	@ApiOperation(value = "我的-查询交接列表", notes = "我的-查询交接列表")
	@RequestMapping(value="/findMyHandoverList",method = RequestMethod.POST)
	public Message findMyHandoverList(@RequestBody BaseParam data) {
		try {
			IPage<TbHrApprovalDimissionApply> selectPage = service.findHandoverList(data);
			return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
		} catch (Exception e) {
			log.error("查询异常:{}",e);
			return Message.error(1,"失败");
		}
	}

	/**
	 * 查询所有交接列表
	 * @author 苏小林
	 * @date 2020/9/9 17:39
	 * @param: data
	 * @return: com.xhsj.user.utils.Message
	 */
	@ApiOperation(value = "查询所有交接列表", notes = "查询所有交接列表")
	@RequestMapping(value="/findAllHandoverList",method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('tb:hr:approval:dimission:handover:all:list')")
	public Message findAllHandoverList(@RequestBody BaseParam data) {
		try {
			IPage<TbHrApprovalDimissionApply> selectPage = service.findAllHandoverList(data);
			return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
		} catch (Exception e) {
			log.error("查询异常:{}",e);
			return Message.error(1,"失败");
		}
	}

	/**
	 * 查询待审核任务
	 * @author 苏小林
	 * @date 2020/9/4 13:22
	 */
	@ApiOperation(value = "查询离职申请待审核任务", notes = "查询离职申请待审核任务")
	@RequestMapping(value="/findDimissionApplyRunTask",method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('tb:hr:approval:dimission:apply:audit')")
	public Message findDimissionApplyRunTask(@RequestBody BaseParam baseParam) {
		try {
			IPage<DimissionApplyAuditTaskVo> iPage = service.findDimissionApplyRunTask(baseParam);
			return Message.success("成功",iPage.getRecords(),iPage.getTotal(),iPage.getPages());
		} catch (Exception e) {
			log.error("查询异常:{}",e);
			return Message.error(1,"失败");
		}
	}


	/**
	 * 查询用户是否提交离职申请 且审核完成
	 * @author 苏小林
	 * @date 2020/9/11 15:13
	 * @param: id
	 */
	@ApiOperation(value = "查询用户是否提交离职申请 且审核完成", notes = "查询用户是否提交离职申请 且审核完成")
	@RequestMapping(value="/findUserWhetherDimission",method = RequestMethod.GET)
	public Message findUserWhetherDimission(@RequestParam(value = "id",required = true) Long id){
		try {
			Map<String,Object> map = service.findUserWhetherDimission(id);
			return Message.success("成功",map);
		} catch (Exception e) {
			log.error("查询异常:{}",e);
			return Message.error(1,"失败");
		}
	}

	/**
	 * 查询离职申请历史任务
	 * @author 苏小林
	 * @date 2020/9/4 17:43
	 * @param: baseParam
	 * @return: com.xhsj.user.utils.Message
	 */
	@ApiOperation(value = "查询离职申请历史任务", notes = "查询离职申请历史任务")
	@RequestMapping(value="/findDimissionApplyHiTask",method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('tb:hr:approval:dimission:apply:audit')")
	public Message findDimissionApplyHiTask(@RequestBody BaseParam baseParam) {
		try {
			System.err.println(LocalDate.now());
			System.err.println(LocalDateTime.now());
			IPage<DimissionApplyAuditTaskVo> iPage = service.findDimissionApplyHiTask(baseParam);
			return Message.success("成功",iPage.getRecords(),iPage.getTotal(),iPage.getPages());
		} catch (Exception e) {
			log.error("查询异常:{}",e);
			return Message.error(1,"失败");
		}
	}

	/**
	 * 流程审核
	 * @author 苏小林
	 * @date 2020/9/4 15:51
	 */
	@ApiOperation(value = "招聘需求申请流程审核", notes = "招聘需求申请流程审核")
	@RequestMapping(value = "/processAudit", method = RequestMethod.POST)
	public Message processAudit(@RequestBody HandleAuditDto handleAuditDto, BindingResult bindingResult) {

		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}

		try {
			service.processAudit(handleAuditDto);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
			return Message.error(1,e.getMessage());
		}

	}

    /**
    * 查询所有数据
    * @author suxiaolin
    * @date Sep 8, 2020 11:41:49 AM
    */
	@ApiOperation(value = "查询hr流程-离职申请表所有数据list", notes = "查询hr流程-离职申请表所有数据list。")
	@RequestMapping(value="/findAll",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:hr:approval:dimission:apply:view')")
	public Message findAll(){
        return Message.success("成功",service.selectAll());
	}
    /**
    * 通过id获取实体类
    * @author suxiaolin
    * @date Sep 8, 2020 11:41:49 AM
    */
	@ApiOperation(value = "通过id获取hr流程-离职申请表实体类", notes = "通过id获取hr流程-离职申请表实体类。")
	@RequestMapping(value="/getById",method = RequestMethod.GET)
	public Message getById(@RequestParam(value="id") Long id) {
    	return Message.success("成功",service.getById(id));
	}

    /**
    * 通过id查询详细信息
    * @author suxiaolin
    * @date Sep 8, 2020 11:41:49 AM
    */
	@ApiOperation(value = "通过id获取实体类详细信息", notes = "通过id获取实体类详细信息。")
	@RequestMapping(value="/detail",method = RequestMethod.GET)
	public Message detail(@RequestParam(value="id") Long id) {
    	return Message.success("成功",service.getById(id));
	}

	   /**
	       * @Description: 离职员工补录保存
	       * @Param:com.xhsj.user.hr.system.dto.SavePostEntryDto
	       * @return:null
	       * @Author: liuqinghua
	       * @Date: 2020/11/10
	       */
	@ApiOperation(value = "离职员工补录保存", notes = "离职员工补录保存")
	@RequestMapping(value="/savePostEntry",method = RequestMethod.POST)
	public Message savePostEntry(@RequestBody SavePostEntryDto data){
		try {
			service.savePostEntry(data);
			return Message.success("成功");
		}catch (Exception e){
			log.error("离职员工补录保存异常",e);
			return Message.error(1,e.getMessage());
		}
	}

}

